// pages/myOrder/myOrder.js
let app = getApp();
import util from '../../../utils/util.js';
import WxParse from '../../../libs/wxParse/wxParse.js';
Page({

  /**
   * 页面的初始数据
   */
   data: {
    states:[{
      label:'全  部',
      id:0
    },{
      label:'待发货',
      id:2
    }],
    selectedState:{id:0},
    orderAll:[],
    orderWait:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
   onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    this.showAllOrderList()
    this.showWaitOrderList()
  },

  //被动
    //显示所有订单
    showAllOrderList(){
      let self = this
      let data = {
        'page': 1,          
        'per_page' : 100,
        'status': 0   
      }
      util.request(util.apiUrl + 'xcx.pin.orderlist', 'POST', data).then(res => {
        let orderAll = res.orders // 订单数组
          /*
            3人拼团状态 : Pin3_status = > {
                null  没有拼团  （人人拼）
                fail  拼团失败
                pining 拼团中
                success 拼团成功
            }
            订单状态: order_statusEnd => {
                0 未付款
                1 待发货（未发货）
                2 待收货（已发货）
                3 已收货（到货）
                6 已经退款
                7  null  没有拼团  （人人拼）
                8  fail  拼团失败  (等待退款)
                9  pining 拼团中
                10  success 拼团成功 （等待卖家发货）
            }
          */
          for (let i = 0; i < orderAll.length; i++) {
            if(orderAll[i].x_order_status == 1){
              //类型  3人拼
              //时间差   
              let timeC = orderAll[i].endtime - orderAll[i].etime 
              if(timeC < 0){
                //超过了24 小时 失败
                // orderWait[i].Pin3_status = 'fail'
                orderAll[i].order_statusEnd = 8
                if(orderAll[i].tuist == 2){
                  orderAll[i].order_statusEnd = 6
                }
              }else{
                //<24 小时 成功、等待成功
                if(orderAll[i].pin_status == 1){
                  // orderWait[i].Pin3_status = 'pining'
                  orderAll[i].order_statusEnd = 9
                }else if(orderAll[i].pin_status == 2){
                  // orderWait[i].Pin3_status = 'success'
                    orderAll[i].order_statusEnd = 10
                    if(orderAll[i].status == 0){
                      orderAll[i].order_statusEnd = 0
                    }else if(orderAll[i].status == 1){
                      orderAll[i].order_statusEnd = 1
                    }else if(orderAll[i].status == 2){
                      orderAll[i].order_statusEnd = 2
                    }else if(orderAll[i].status == 3){
                      orderAll[i].order_statusEnd = 3
                    }
                }
              }
            }else{
              //类型  人人拼
              // orderWait[i].Pin3_status = 'null'
              orderAll[i].order_statusEnd = 7
              if(orderAll[i].status == 0){
                orderAll[i].order_statusEnd = 0
              }else if(orderAll[i].status == 1){
                orderAll[i].order_statusEnd = 1
              }else if(orderAll[i].status == 2){
                orderAll[i].order_statusEnd = 2
              }else if(orderAll[i].status == 3){
                orderAll[i].order_statusEnd = 3
              }
            }
          }
        console.log(orderAll)
        self.setData({
          'orderAll':orderAll
        })
        wx.hideLoading()
      }).catch(err => {
        console.log(err)
      });
    },

    //显示待发货
    showWaitOrderList(){
      let self = this
      let data = {
        'page': 1,
        'per_page': 100,
        'status': 1
      }
      util.request(util.apiUrl + 'xcx.pin.orderlist', 'POST', data).then(res => {
        let orderWait = res.orders

        let arrWait = []
          /*
            3人拼团状态 : Pin3_status = > {
                null  没有拼团  （人人拼）
                fail  拼团失败
                pining 拼团中
                success 拼团成功
            }
            订单状态: order_statusEnd => {
                0 未付款
                1 待发货（未发货）
                2 待收货（已发货）
                3 已收货（到货）
                6 已经退款
                7  null  没有拼团  （人人拼）
                8  fail  拼团失败  (等待退款)
                9  pining 拼团中
                10  success 拼团成功 （等待卖家发货）
            }
          */
          for (let i = 0; i < orderWait.length; i++) {
            if(orderWait[i].x_order_status == 1){
              //类型  3人拼
              //时间差   
              let timeC = orderWait[i].endtime - orderWait[i].etime 
              if(timeC < 0){
                //超过了24 小时 失败
                // orderWait[i].Pin3_status = 'fail'
                orderWait[i].order_statusEnd = 8

                if(orderWait[i].tuist == 2){
                  orderWait[i].order_statusEnd = 6
                }
              }else{
                //<24 小时 成功、等待成功
                if(orderWait[i].pin_status == 1){
                  // orderWait[i].Pin3_status = 'pining'
                  orderWait[i].order_statusEnd = 9
                }else if(orderWait[i].pin_status == 2){
                  // orderWait[i].Pin3_status = 'success'
                  orderWait[i].order_statusEnd = 10
                  if(orderWait[i].status == 0){
                    orderWait[i].order_statusEnd = 0
                  }else if(orderWait[i].status == 1){
                    orderWait[i].order_statusEnd = 1
                  }else if(orderWait[i].status == 2){
                    orderWait[i].order_statusEnd = 2
                  }else if(orderWait[i].status == 3){
                    orderWait[i].order_statusEnd = 3
                  }
                }
              }
            }else{
              //类型  人人拼
              // orderWait[i].Pin3_status = 'null'
              orderWait[i].order_statusEnd = 7
              if(orderWait[i].status == 0){
                orderWait[i].order_statusEnd = 0
              }else if(orderWait[i].status == 1){
                orderWait[i].order_statusEnd = 1
              }else if(orderWait[i].status == 2){
                orderWait[i].order_statusEnd = 2
              }else if(orderWait[i].status == 3){
                orderWait[i].order_statusEnd = 3
              }
            }
          }
          console.log(orderWait)

          for(let i = 0;i < orderWait.length;i++){
            
            if (orderWait[i].order_statusEnd == 1){
              arrWait.push(orderWait[i])
            }
          }
          console.log(arrWait)
          
        self.setData({
          'orderWait': arrWait
        })
        wx.hideLoading()
      }).catch(err => {
        console.log(err)
      });
    },
  //被动 End
  //主动 
    //切换 全部 - 待发货
    tabChange(Param) {
      let index = Param.target.dataset.index
      this.setData({
        'selectedState.id': index
      })
    },
    //确认收货
    orderConfirmed(Param){

      // Param.target.dataset.index
      let id = Param.target.dataset.id
      console.log(id)
      let data = {
        'order' : id
      }
      //模态框 显示确认收货
      wx.showLoading({
        title: '加载中',
      })
      console.log(data)

      util.request(util.apiUrl + 'xcx.pin.ordershouhuo', 'POST', data).then(res => {
        console.log('orderConfirmed',res)
        let error_code = res.error_code

        if(error_code == 404){
          //确认收货失败
          wx.showToast({
            title: '确认收货失败',
            icon: 'loading',
            duration: 1000
          })
        }else{
          //已经成功了
          wx.showToast({
            title: '确认收货成功',
            icon: 'success',
            duration: 1000
          })
        }
      }).catch(err => {
        console.log(err)
      });
      this.showAllOrderList()
      this.showWaitOrderList()
    }
  //主动 ~ END
})